package com.molichuxing.services.infrastructure.service.impl;

import com.molichuxing.services.infrastructure.bean.PaymentDetailBean;
import com.molichuxing.services.infrastructure.convertor.Convert;
import com.molichuxing.services.infrastructure.dao.PaymentDetailMapper;
import com.molichuxing.services.infrastructure.dto.request.create.PaymentDetailCreateDto;
import com.molichuxing.services.infrastructure.dto.response.PaymentDetailDownloadDto;
import com.molichuxing.services.infrastructure.dto.response.PaymentDetailDto;
import com.molichuxing.services.infrastructure.service.PaymentDetailService;
import com.molichuxing.services.property.PaymentDetailTypeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 * 支付详情 服务实现类
 * </p>
 *
 * @author zhaopenghui
 * @since 2019-08-23
 */
@Service("paymentDetailService")
public class PaymentDetailServiceImpl implements PaymentDetailService {

    @Autowired
    PaymentDetailMapper paymentDetailMapper;

    /**
     * 查询支付详情
     *
     * @param paymentId 支付id
     * @return
     */
    @Override
    public List<PaymentDetailDto> getPaymentDetail(Integer paymentId) {
        List<PaymentDetailBean> paymentDetailBeans = paymentDetailMapper.getPaymentDetail(paymentId, null);
        return Convert.toPaymentDetailDto(paymentDetailBeans);
    }

    /**
     * 查询支付详情
     *
     * @param paymentId 支付id
     * @param type      类型
     * @return
     */
    @Override
    public PaymentDetailDto getPaymentDetail(Integer paymentId, PaymentDetailTypeEnum type) {
        List<PaymentDetailBean> paymentDetailBeans = paymentDetailMapper.getPaymentDetail(paymentId, type.getValue());
        if (paymentDetailBeans == null || paymentDetailBeans.isEmpty()) {
            return null;
        }
        return Convert.toPaymentDetailDto(paymentDetailBeans.get(0));
    }

    /**
     * 查询支付详情
     *
     * @param type 类型
     * @return
     */
    @Override
    public List<PaymentDetailDto> getPaymentDetail(PaymentDetailTypeEnum type) {
        return Convert.toPaymentDetailDto(paymentDetailMapper.getPaymentDetailByType(type.getValue()));
    }

    /**
     * 批量新增
     *
     * @param paymentDetails
     * @return
     */
    @Override
    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, timeout = 36000, rollbackFor = Exception.class)
    public Boolean createBatch(List<PaymentDetailCreateDto> paymentDetails) {
        if (paymentDetailMapper.createBatch(Convert.toPaymentDetailBean(paymentDetails)) > 0) {
            return true;
        }
        return false;
    }

    /**
     * 查询保证金
     *
     * @param paymentIds
     * @return
     */
    @Override
    public List<PaymentDetailDto> getDeposit(List<Integer> paymentIds) {
        return Convert.toPaymentDetailDto(
                paymentDetailMapper.getDeposit(paymentIds, PaymentDetailTypeEnum.DEPOSIT.getValue()));
    }

    /**
     * 支付导出查询
     *
     * @param paymentIds 支付单ids
     * @return
     */
    @Override
    public List<PaymentDetailDownloadDto> getPaymentDetailDownload(List<Integer> paymentIds) {
        return paymentDetailMapper.getPaymentDetailDownload(paymentIds);
    }
}
